
const { ShowCardProcessMgr } = require("./showCardProcessMgr");

class DXShowCardProcessMgr extends ShowCardProcessMgr {

    constructor() {
        super();
        this.totalLoseScore = 0;
    }

    getWinnerIndex() {
        var winnerIndex = null;
        var winValue = 0;
        var gamberIndexs = this.seatMgr.getGamberIndexsByBanker(this.banker);

        for (let i = 0; i < gamberIndexs.length; ++i) {
            let index = gamberIndexs[i];
            let gamber = this.seatMgr.getGamberBySeatIndex(index);
            if (gamber.scoreBetting == 0) continue;     // 玩家选择了弃
            let value = this.cardMgr.calculate(gamber.holds);
            console.log("玩家", gamber.userId, "的手牌是", gamber.holds, "得分是", value);
            if (winnerIndex == null || value > winValue) {
                winnerIndex = index;
                winValue = value;
            }
        }
        return winnerIndex ? winnerIndex : gamberIndexs[gamberIndexs.length-1];
    }
    
    doAction() {
        super.doAction();
        var totalLoseScore = 0;
        var winnerIndex = this.getWinnerIndex();
        var gamberIndexs = this.seatMgr.getGamberIndexs();
        for (var i = 0; i < gamberIndexs.length; ++i) {
            let index = gamberIndexs[i];
            let gamber = this.seatMgr.getGamberBySeatIndex(index);
            if (gamber.scoreBetting == 0) continue;     // 玩家选择了弃
    
            if (index != winnerIndex) {
                gamber.score = -gamber.scoreBetting;
                totalLoseScore += gamber.scoreBetting;
            } else {
                gamber.score = Math.min(gamber.scoreBetting, totalLoseScore);
            }
        }
        var winner = this.seatMgr.getGamberBySeatIndex(winnerIndex);
        winner.score = Math.min(winner.scoreBetting, totalLoseScore);

        if (totalLoseScore > winner.scoreBetting) {
            // 多余的失分需要投入底池
            this.gameMgr.AddFundPool(totalLoseScore - winner.scoreBetting);
        }
    }
}